<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>read manual page - Tcl Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> read</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
read &mdash; Read from a channel
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>read </B>?<B>-nonewline</B>? <I>channelId</I><BR>
<B>read </B><I>channelId numChars</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
In the first form, the <B>read</B> command reads all of the data from
<I>channelId</I> up to the end of the file.  If the <B>-nonewline</B>
switch is specified then the last character of the file is discarded
if it is a newline.  In the second form, the extra argument specifies
how many characters to read.  Exactly that many characters will be
read and returned, unless there are fewer than <I>numChars</I> left in
the file; in this case all the remaining characters are returned.  If
the channel is configured to use a multi-byte encoding, then the
number of characters read may not be the same as the number of bytes
read.
<P>
<I>ChannelId</I> must be an identifier for an open channel such as the
Tcl standard input channel (<B><A HREF="../TclLib/GetStdChan.htm">stdin</A></B>), the return value from an
invocation of <B><A HREF="../TclCmd/open.htm">open</A></B> or <B><A HREF="../TclCmd/socket.htm">socket</A></B>, or the result of a channel
creation command provided by a Tcl extension. The channel must have
been opened for input.
<P>
If <I>channelId</I> is in nonblocking mode, the command may not read as
many characters as requested: once all available input has been read,
the command will return the data that is available rather than
blocking for more input.  If the channel is configured to use a
multi-byte encoding, then there may actually be some bytes remaining
in the internal buffers that do not form a complete character.  These
bytes will not be returned until a complete character is available or
end-of-file is reached.  The <B>-nonewline</B> switch is ignored if
the command returns before reaching the end of the file.
<P>
<B>Read</B> translates end-of-line sequences in the input into
newline characters according to the <B>-translation</B> option
for the channel.
See the <B><A HREF="../TclCmd/fconfigure.htm">fconfigure</A></B> manual entry for a discussion on ways in
which <B><A HREF="../TclCmd/fconfigure.htm">fconfigure</A></B> will alter input.
<H3><A NAME="M5">USE WITH SERIAL PORTS</A></H3>
For most applications a channel connected to a serial port should be
configured to be nonblocking: <B><A HREF="../TclCmd/fconfigure.htm">fconfigure</A></B><I> channelId </I><B>-blocking
</B><I>0</I>.  Then <B>read</B> behaves much like described above.  Care
must be taken when using <B>read</B> on blocking serial ports:
<P>
<DL class="use with serial ports">
<DT><A NAME="M6"><B>read </B><I>channelId numChars</I></A><DD>
In this form <B>read</B> blocks until <I>numChars</I> have been received
from the serial port.
<P><DT><A NAME="M7"><B>read </B><I>channelId</I></A><DD>
In this form <B>read</B> blocks until the reception of the end-of-file
character, see <B><A HREF="../TclCmd/fconfigure.htm">fconfigure</A></B> <B>-eofchar</B>. If there no end-of-file
character has been configured for the channel, then <B>read</B> will
block forever.
<P></DL>
<H3><A NAME="M8">EXAMPLE</A></H3>
This example code reads a file all at once, and splits it into a list,
with each line in the file corresponding to an element in the list:
<P>
<PRE>set fl [open /proc/meminfo]
set data [<B>read</B> $fl]
close $fl
set lines [split $data &#92;n]</PRE>
<H3><A NAME="M9">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/file.htm">file</A></B>, <B><A HREF="../TclCmd/eof.htm">eof</A></B>, <B><A HREF="../TclCmd/fblocked.htm">fblocked</A></B>, <B><A HREF="../TclCmd/fconfigure.htm">fconfigure</A></B>, <B><A HREF="../TclLib/StdChannels.htm">Tcl_StandardChannels</A></B>
<H3><A NAME="M10">KEYWORDS</A></H3>
<A href="../Keywords/B.htm#blocking">blocking</A>, <A href="../Keywords/C.htm#channel">channel</A>, <A href="../Keywords/E.htm#end of line">end of line</A>, <A href="../Keywords/E.htm#end of file">end of file</A>, <A href="../Keywords/N.htm#nonblocking">nonblocking</A>, <A href="../Keywords/R.htm#read">read</A>, <A href="../Keywords/T.htm#translation">translation</A>, <A href="../Keywords/E.htm#encoding">encoding</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
</div>
</BODY></HTML>
